package com.prs.crm.action.doc;

import java.io.File;
import java.util.Collection;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;

import com.prs.crm.annotation.Clew;
import com.prs.crm.domain.doc.DocLevel;
import com.prs.crm.domain.doc.Document;
import com.prs.crm.domain.sys.Code;
import com.prs.crm.domain.sys.CodeCommon;
import com.prs.crm.service.doc.DocLevelService;
import com.prs.crm.service.doc.DocumentService;
import com.prs.crm.service.sys.CodeCommonService;
import com.prs.crm.service.sys.CodeService;

public class DocumentAction extends BaseAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Document document;
	private DocumentService documentService;
	private DocLevelService docLevelService;
	private PaginationSupport pagination;
	
	private Integer[] ids;
	private Collection<DocLevel> docLevels;
	private File upload;
	private String uploadFileName;
	private CodeCommon codeCommon;
	private CodeCommonService  codeCommonService;
	private String Encode;
	private Code code;
	private CodeService codeService;
	
	private Collection<CodeCommon> codelist;
	@Action("editDocument")
	public String editDocument() {
		docLevels = this.getDocLevelService().getAllDocLevel();
		return SUCCESS;
	}
	@Action("editUpdateTask")
	public String editUpdateTask(){
		docLevels = this.getDocLevelService().getAllDocLevel();
		this.codelist  =this.getCodeCommonService().getCodeCommonByCodeType("文档");
		return SUCCESS;
	}
	
	@Action("editWorkTask")
	public String editWorkTask(){
		this.codelist  =this.getCodeCommonService().getCodeCommonByCodeType("文档");	
		return SUCCESS;
	}
	@Action("editWorkTaskCreat")
	public String editWorkTaskCreat() {
			this.codelist  =this.getCodeCommonService().getCodeCommonByCodeType("文档");
			return SUCCESS;
	}

	@Action("listHandDocument")
	public String listHandDocument() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(Document.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		criteria.addOrder(Order.desc("recordTime"));
		this.setPagination(this.getDocumentService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	@Clew(results={"success"})
	@Action(value="submitDocument",results={@Result(name="success",type="redirect",location="listDocument"),
			@Result(name="error",type="chain",location="editDocument")})
	public String submitDocument() {
		if(document.getName() == null || document.getName().equals("")) {
			this.addActionError("请输入文档名称");
			return ERROR;
		}
		if(document.getDocumentcode()== null || document.getDocumentcode().equals("")) {
			this.addActionError("请输入文档编码");
			return ERROR;
		}
		if(document.getType() == null) {
			this.addActionError("请选择文档类型");
			return ERROR;
		}
		if(upload == null) {
			this.addActionError("请选择文档");
			return ERROR;
		}
		if ("n".equals(Encode)) {
			Code codes = new Code();
			codes.setCodeValue(document.getDocumentcode());
			codes.setEntityName(this.getClass().getName());
			codes.setCodeType("手动编码");
			codes.setCodeMolde("文档");
			this.getCodeService().save(codes);
			document.setCode(codes);
			
		} else {
			CodeCommon Vob = this.getCodeCommonService().getVobject(
					codeCommon.getId());
			Code codes = new Code();
			codes.setEntityName(this.getClass().getName());
			codes.setCodeValue(document.getDocumentcode());
			Vob.setNumBackUp(Increase(Vob.getNumBackUp()));
			this.codeCommonService.saveOrUpdate(Vob);
			codes.setCodeType(Vob.getCodePlan());
			codes.setCodeMolde("文档");
			this.getCodeService().save(codes);
			document.setCode(codes);
		}
		this.getDocumentService().submitDocument(document);
		this.setPrompt("提交文档成功");
		return SUCCESS;
	}
	
	@Action("viewDocument")
	public String viewDocument(){
		return SUCCESS;
	}
	
	@Clew(results={"success"})
	@Action(value="updateTask",results={@Result(name="success",type="redirect",location="listHandDocument"),
			@Result(name="error",type="chain",location="listHandDocument")})
	public String updateTask() {
        Document docum=this.getDocumentService().getDocument(document.getId());
		docum.setName(document.getName());
		docum.setType(document.getType());
		docum.setLevel(document.getLevel());
		docum.setDocumentcode(document.getDocumentcode());
		this.getDocumentService().saveOrUpdate(document);
		this.setPrompt("修改文档成功");
		return SUCCESS;
	}
	@Clew(results={"success"})
	@Action(value="delTask",results={@Result(name="success",type="redirect",location="listHandDocument"),
			@Result(name="error",type="chain",location="editUpdateTask?document=${document.getId()}")})
	public String delTask() {
		this.getDocumentService().batchRemove(ids);	
		this.setPrompt("删除文档成功");
		return SUCCESS;
	}
	
	public Document getDocument() {
		return document;
	}

	public void setDocument(Document document) {
		this.document = document;
	}
	public DocumentService getDocumentService() {
		return documentService;
	}
	public void setDocumentService(DocumentService documentService) {
		this.documentService = documentService;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public DocLevelService getDocLevelService() {
		return docLevelService;
	}

	public void setDocLevelService(DocLevelService docLevelService) {
		this.docLevelService = docLevelService;
	}

	public Collection<DocLevel> getDocLevels() {
		return docLevels;
	}

	public void setDocLevels(Collection<DocLevel> docLevels) {
		this.docLevels = docLevels;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public CodeCommon getCodeCommon() {
		return codeCommon;
	}
	public void setCodeCommon(CodeCommon codeCommon) {
		this.codeCommon = codeCommon;
	}
	public CodeCommonService getCodeCommonService() {
		return codeCommonService;
	}
	public void setCodeCommonService(CodeCommonService codeCommonService) {
		this.codeCommonService = codeCommonService;
	}
	public String getEncode() {
		return Encode;
	}
	public void setEncode(String encode) {
		Encode = encode;
	}
	public Code getCode() {
		return code;
	}
	public void setCode(Code code) {
		this.code = code;
	}
	public CodeService getCodeService() {
		return codeService;
	}
	public void setCodeService(CodeService codeService) {
		this.codeService = codeService;
	}
	public Collection<CodeCommon> getCodelist() {
		return codelist;
	}
	public void setCodelist(Collection<CodeCommon> codelist) {
		this.codelist = codelist;
	}
	public static String Increase(String backupNum){
		String Inc="";
		int lengths=backupNum.length();
	    String lengthss=(Integer.parseInt(backupNum)+1)+"";
	    int increase=(lengthss.trim()).length();
	    if(lengths!=increase){
	    	String In="";
	    	for(int i=0;i<lengths-increase;i++){
	    		In=In+"0";
	    	}
	    	In=(In+lengthss).trim();
	    	Inc=In;
	    }else{
	    	Inc=lengthss;
	    }
	    return Inc.trim();
	}
}
